package com.hzit.net;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 底层调用 get和post请求工具类
 */
public class WXNetUtil {


    /**
     * 发送get请求，参数通过url传递
     *
     * @param url
     * @return
     */
    public static String get(String url) {
        //1.获取HttpClient对象
        HttpClient ie = HttpClients.createDefault();

        //2.创建需要发起的请求
        HttpGet httpGet = new HttpGet(url);


        try {
            //3.执行
            //4.获取结果
            HttpResponse response = ie.execute(httpGet);

            //5.处理结果
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity, "UTF-8");

            //System.out.println(body);

            return body;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpGet.releaseConnection();
        }

        return null;
    }


    /**
     * POST请求
     * 除了URL需要请求体参数传递
     *
     * @param url
     * @param requestBody
     * @return
     */
    public static String post(String url, String requestBody) {

        //1.获取HttpClient对象
        HttpClient ie = HttpClients.createDefault();

        //2.创建需要发起的请求
        //HttpGet httpGet = new HttpGet(url);
        HttpPost httpPost = new HttpPost(url);
        //设置请求头信息
        httpPost.addHeader("contentType", "application/json");

        //设置请求体信息
        StringEntity se = new StringEntity(requestBody, "UTF-8");
        se.setContentType("text/json");
        //se.setContentType(CONTENT_TYPE_TEXT_JSON);
        httpPost.setEntity(se);

        try {
            //3.执行
            //4.获取结果
            HttpResponse response = ie.execute(httpPost);

            //5.处理结果
            HttpEntity entity = response.getEntity();
                       String body = EntityUtils.toString(entity, "UTF-8");

            //System.out.println(body);

            return body;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpPost.releaseConnection();
        }

        return null;
    }

    public static void main(String[] args) {
        //https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx405f68216288b619&secret=8d88a36a031b1f6b83b9cb29399221e7";
        //String body = get(url);
        //System.out.println(body);

        //有效期两个小时，过期重新获取
//        String token = "39_0BPN0o1K-2JynjQOOALuCtbAzoD0GJmwCBe7ccVuyIVOx0vSld_UFTBl6voaYPLx0fAHlCb57zgZuOhDDAO4CkBTtTTg5m-_F5TpV5hlpubquBrvoVTu2mUvHnI8HPOwLlEWQzAcnda2wrbdIUFdACAXJU";
//        String url ="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token;
//        String param = "{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"bj2004_id_001\"}}}";
//
//        String post = post(url, param);
//        System.out.println(post);

       // {"ticket":"gQFE8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyaUNMV2dwRGtkUTMxQ2dnWGh2Y1MAAgQQFrJfAwSAOgkA","expire_seconds":604800,"url":"http:\/\/weixin.qq.com\/q\/02iCLWgpDkdQ31CggXhvcS"}



        String tt = "{\n" +
                "\t\"news\": {\n" +
                "\t\t\"articles\": [{\n" +
                "\t\t\t\"picurl\": \"https://img.meituan.net/msmerchant/d3aa8cbedccf51f30c81b71417f12254711607.jpg\",\n" +
                "\t\t\t\"description\": \"宝安区新沙路万科翡逸郡园四期116铺\",\n" +
                "\t\t\t\"title\": \"炭家牛韩式烤肉\",\n" +
                "\t\t\t\"url\": \"https://www.meituan.com/meishi/109085717/\"\n" +
                "\t\t}]\n" +
                "\t},\n" +
                "\t\"touser\": \"oJ1R0w4jz1QaGUMn8SA5n9ud-yIA\",\n" +
                "\t\"msgtype\": \"news\"\n" +
                "}";
         String myurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=39_zJqiG0l-rYIp1TQuPjSQQbwnY1tUKHHeGnZJzWUS-CTUyJIz8no_MsvLlSkw44PkpS2xk5ZwyJvdXEyVjR0TA7R66cxyqZcLk1SaJafSYAZGWxK4fse32UJxxVEuXgRLpmupd5ZpFqfaFuC6QUUaAJAYCC";

        String post1 = post(myurl, tt);
        System.out.println(post1);

    }


}
